<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/6 0006
 * Time: 18:40
 */

namespace app\index\controller;

use think\Controller;
use lib\Transfers;
use think\Db;
use think\Exception;
use app\index\model\Contact;

class Pay extends Base
{   
    public function showbalance()
    {
        $id = input('id'); 
        $sign = input('sign');//0普通vip1高级vip
        $status = $this->status;
        if($status == "error"){
            $dat = ['code' => 403, 'msg' => '登录超时,请重新登录'];
            return json_encode($dat);
        }
        else
        {   $txmoney = Contact::where('id',1)->value('money'); //提现标准
            $balance = db('user')->where('id',$id)->value('balance');//账户余额
            $dat = array('code'=>200,'msg'=>'返回数据成功','balance'=>$balance,'txmoney'=>$txmoney);
            echo json_encode($dat,JSON_FORCE_OBJECT);
        }
    }

    public function pay()
    {
        $mchid = '1520441161';          //微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送
        $appid = 'wxd175ae1e69006837';  //微信支付申请对应的公众号的APPID
        $appKey = '26c09900c47fa037457f5aad8c023a4a';   //微信支付申请对应的公众号的APP Key
        $apiKey = 'w3236281477qqcomQ445256215175508';   //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
        //获取提现金额
        $txmoney = Contact::where('id',1)->value('money');
        //①、获取当前访问页面的用户openid（如果给指定用户转账，则直接填写指定用户的openid)
        $wxPay = new Transfers($mchid,$appid,$appKey,$apiKey);
        $openId = $wxPay->GetOpenid();//获取openid

        if(!$openId){
            return json_encode(['code'=>400,'msg'=>'获取openid失败']);
        }else{
            //②、付款
            $id = $_GET['id'];  //当前用户id
            $outTradeNo = uniqid();     //订单号
            $payAmount = $_GET['price'];//提现金额，单位:元。转账最小金额为1元
            $trueName = $_GET['phone'];//收款人真实姓名

            $op = db('user')->where('id',$id)->value('openid');           
                //判断是不是这个微信号
                if($op == $openId){
                    $data = db('user')->where('id',$id)->value('balance'); //账户余额
                     // 判断账户余额是否大于提现标准
                    if($data >= $payAmount ){
                        if($payAmount >= $txmoney){//判断提现金额是否大于余额
                           Db::startTrans();
                           $result = $wxPay->createJsBizPackage($openId,$payAmount,$outTradeNo,$trueName);
                           if( $result == 1){
                                try {
                                    Db::table('zq_account')->insert(['rid'=>$id,'price'=>$payAmount,'truename'=>$trueName,'tradeno'=>$outTradeNo,'created_at'=> date('Y-m-d H:i:s', time())]);       
                                    Db::table('zq_user')->where('id',$id)->setDec('balance',$payAmount);
                                    $acc =  db('user')->where('id',$id)->value('balance');
                                    return json_encode(['code'=>200,'msg'=>'提现成功','account'=>$acc]);
                                } catch (Exception $e) {
                                Db::rollback();
                                    echo $e->getMessage();
                                }

                           } else {

                                return json_encode(['code'=>502,'msg'=>'请联系平台打款']);
                           }

                          
                         
                           // if( $jspay == "")
                            // try{                      
                            //     Db::table('zq_account')->insert(['rid'=>$id,'price'=>$payAmount,'truename'=>$trueName,'tradeno'=>$outTradeNo,'created_at'=> date('Y-m-d H:i:s', time())]);       
                            //     Db::table('zq_user')->where('id',$id)->setDec('balance',$payAmount); 
                            //     return $wxPay->createJsBizPackage($openId,$payAmount,$outTradeNo,$trueName);
                            //     exit;
                            //     $acc =  db('user')->where('id',$id)->value('balance');
                            //     return json_encode(['code'=>200,'msg'=>'提现成功','account'=>$acc]);
                               
                                
                            // }catch (\Exception $e){
                            //     Db::rollback();
                            //     echo $e->getMessage();
                            //     return json_encode(['code'=>502,'msg'=>'请联系平台']);
                    
                            // }
                        }else{
                             return json_encode(['code'=>501,'msg'=>'提现金额最少'.$txmoney.'元']);
                        }
                    }else{
                        return json_encode(['code'=>500,'msg'=>'账户余额不足']);
                    }
                 }else{
                     return json_encode(['code'=>400,'msg'=>'请用注册时的手机号码绑定的微信进行提现操作']);
                 }
            
        }
    }
}